import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import '../../../base/SilScaffold.dart';
import '../../../common/Method/CommonMethod.dart';
import '../../../common/widgets/cubitWidget/DividingLine.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../Home/Controller/AppManagerController.dart';
import '../Language/LanguageController.dart';
import '../Language/SilLocalizations/SilLocalizations.dart';
import 'AMItemContentView.dart';

/**
 * 关于我们
 */

class AboutUsPage extends StatefulWidget {
  const AboutUsPage({Key? key}) : super(key: key);

  @override
  _AboutUsPageState createState() => _AboutUsPageState();
}

class _AboutUsPageState extends State<AboutUsPage> {
  AppManagerController _appManagerController = Get.put(AppManagerController());

  /// 当前语言
  LanguageController _languageController =
      Get.put<LanguageController>(LanguageController());
  bool _isPromptEnglish = true;

  @override
  void initState() {
    super.initState();
    _isPromptEnglish = _languageController.isPromptEnglish();

    Future.delayed(Duration.zero, () {
      /// 获取最新app版本号和当前版本号
      _appManagerController.getAppVersion();
      _appManagerController.getLastAppVersion();
    });
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
        navBarHide: false,
        sideMargin: 0,
        title: LangOf(context)?.mine?.about_us,
        body: mainWidget());
  }

  mainWidget() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // logo and slogan
        Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(bottom: 50.w),
            child: SilImage(
              _isPromptEnglish
                  ? 'imgs/login/logoEN.png'
                  : 'imgs/login/logo.png',
              width: 220.w,
              height: 90.w,
            )),

        /// 其他信息(
        elseInfoView(),

        /// 华丽的分割线
        DividingLine(
            margin: EdgeInsets.only(top: 20.w, left: 15.w, right: 15.w)),

        /// 公司地址
        addressView()
      ],
    );
  }

  /// 其他信息
  elseInfoView() {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 30.w),
        child: Column(
          children: [
            AMItemContentView(
              title: LangOf(context)?.aboutUs?.version ?? '',
              titleFontWidget: FontWeight.w400,
              rightView: Obx(() => SilText(
                    _appManagerController.appVersion.value,
                    color: ColorUtils.color_font_main,
                    fontSize: 16.sp,
                    textAlign: TextAlign.right,
                    margin: EdgeInsets.only(right: 8.w),
                  )),
            ),

            /// 版本更新
            Obx(() => versionUpdateWidget()),

            AMItemContentView(
              title: LangOf(context)?.aboutUs?.service, //'服务协议',
              titleFontWidget: FontWeight.w400,
              margin: EdgeInsets.only(top: 26.w),
              onTapFunc: () {
                gotoServerTocolPage(context);
              },
            ),
            AMItemContentView(
              title: LangOf(context)?.aboutUs?.policy, //'隐私政策',
              titleFontWidget: FontWeight.w400,
              margin: EdgeInsets.only(top: 26.w),
              onTapFunc: () {
                gotoProTocolPrivacyPage(context);
              },
            ),
          ],
        ));
  }

  /// 版本更新
  versionUpdateWidget() {
    String lastVersionName =
        _appManagerController.lastVersionName?.value ?? '0';
    lastVersionName = lastVersionName.replaceAll('.', '');
    String curVersionName = _appManagerController.appVersion.value;
    curVersionName = curVersionName.replaceAll('.', '');
    if (lastVersionName == '') lastVersionName = '0';
    bool isNeedUpate = num.parse(lastVersionName) > num.parse(curVersionName);

    /// 检查更新
    String? noNewVersion = LangOf(context)?.aboutUs?.noNewVersion;

    return AMItemContentView(
      title: LangOf(context)?.aboutUs?.update,
      subTitle: isNeedUpate ? '' : noNewVersion,
      delRightArrow: !isNeedUpate,
      titleFontWidget: FontWeight.w400,
      margin: EdgeInsets.only(top: 26.w),
      onTapFunc: () {
        if (isNeedUpate) _appManagerController.gotoAppDownloadPage(context);
      },
    );
  }

  /// 介绍/地址
  addressView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SilText(
            LangOf(context)?.aboutUs?.function_introduction,
            margin: EdgeInsets.symmetric(vertical: 15.w),
            color: ColorUtils.color_font_main,
            fontSize: 16.sp,
            fontWeight: FontWeight.w400,
            maxLines: 5,
          ),
          SilText(
            LangOf(context)?.concactUs?.address,
            margin: EdgeInsets.symmetric(vertical: 15.w),
            color: ColorUtils.color_font_main,
            fontSize: 16.sp,
            fontWeight: FontWeight.w400,
            maxLines: 0,
          )
        ],
      ),
    );
  }
}
